<template>
    <button class="loginBtn" 
		@getuserinfo='getUserInfo' 
		open-type="getUserInfo" 
		type="default" 
		:style="{
			borderRadius:btnRadius,
			background:btnBj,
			height:btnHeight,
			color:fontColor,
			border:border,
			lineHeight:btnHeight,
			fontSize:fontSize
		}"
		@click="clickBtn">
		<image :src="butImg" v-if="butImg!=''"></image>
    	{{btnTitle}}
    </button>
</template>

<script>
    export default {
        name: 'authorLogin',
        props: {
            //标题
            title: {
            	type: String,
            	default: function() {
            		return '';
            	}
            },
			// 高度，单位任意
			height: {
				type: [String, Number],
				default: 'auto'
			},
			// 圆角，单位任意
			borderRadius: {
				type: [String, Number],
				default: 0
			},
			// 边框
			border: {
				type: String,
				default: ''
			},
			//导航背景色，支持渐变
			bgColor: {
				type: [String, Array],
				default: function() {
					return '#FFFFFF';
				}
			},
			// 字体颜色
			fontColor:{
				type: [String, Array],
				default: function() {
					return '#FFFFFF';
				}
			},
			// 字体颜色
			fontSize:{
				type: String,
				default: function() {
					return '';
				}
			},
			// button里面的图片
			butImg:{
				type: String,
				default: function() {
					return '';
				}
			}
        },
        data() {
            return {
                ref:'',
				//标题
				btnTitle: '',
				// 高
				btnHeight:'',
				// 背景
				btnBj:'',
				// 圆角
				btnRadius:0,
				// 图片
				btnImg:'',
            };
        },
		mounted: function(option) {
			var _this = this;
			uni.getStorage({
				key: 'refVal',
				success: function (res) {
					_this.ref = res.data;
				}
			});
		},
		watch:{
			title(val) {
				this.btnTitle = val;
			},
			height(val){
				this.btnHeight = val;
			},
			borderRadius(val){
				this.btnRadius = val;
			},
			bgColor(val){
				this.btnBj = val;
			}
		},
		created(){
			this.btnTitle = this.title;
			this.btnHeight = this.height;
			this.btnRadius = this.borderRadius;
			this.btnBj = this.bgColor;
		},
        methods: {
			clickBtn(){
				this.$emit('clickAgain');
			},
            getUserInfo(e) {
				let _this = this;
				let ret=e.detail;
				
				if (uni.getStorageSync('TOKEN')) {//有token
					this.testToken('/site/get-info',{}).then(res => {
						if(res.data.status!=200){//但token无效
							if(ret.errMsg == "getUserInfo:fail auth deny") {
								uni.showModal({
									content:"为了更好的服务，请同意授权"
								})
							}else if(ret.errMsg == "getUserInfo:ok") {
								_this.userLogin(e.detail);
							}else {
								uni.showModal({
									content:"授权失败"
								})
							}
						}
					})
				}else{//无TOKRN
					if(ret.errMsg == "getUserInfo:fail auth deny") {
						uni.showModal({
							content:"为了更好的服务，请同意授权"
						})
					}else if(ret.errMsg == "getUserInfo:ok") {
						_this.userLogin(e.detail);
					}else {
						uni.showModal({
							content:"授权失败"
						})
					}
				}
            },
			userLogin(e) {
				let userInfo = {
					nickname: e.userInfo.nickName,
					gender: e.userInfo.gender,
					language: e.userInfo.language,
					city: e.userInfo.city,
					province: e.userInfo.province,
					country: e.userInfo.country,
					avatar: e.userInfo.avatarUrl
				};
				let _this = this ;
				uni.login({
					success: res => {
						let code = res.code;
						if(code){
							//登录参数
							let data={
								code: code,
								ref: _this.ref
							}
							_this.apipost('/wxxcx/get-user-login',data).then(res =>{
								if(res.data.status == 200) {
									uni.setStorage({
										key:'TOKEN',
										data:res.data.data.token,
										success: function () {
											_this.apipost('/fun/member/update-user-info',userInfo).then(res =>{
												if(res.data.status == 200) {
													uni.showToast({
														title:'登录成功',
														icon:'success',
														success: res => {
															uni.getStorage({
															    key: 'storerOrder',
															    success: function (res) {
																	uni.removeStorage({
																		key: 'storerOrder'
																	});
															        setTimeout(function (){
															        	uni.reLaunch({//+'&storer='+res.data.storer
															        		url: '../../pages/order/orderInfoPayment?id='+res.data.orderId
															        	});
															        }, 1000);
															    }
															});
														},
													})
												}
											}) 
										}
									})
								}
							}) 
						}
					},
					fail: () => {
						uni.showModal({
							content:"没有获取到登陆信息，请重试"
						})
					}
				});
			}
        }
    }
</script>

<style lang="scss">
.loginBtn{
	font-size: 32rpx;
}
.loginBtn::after{ border: none; }
</style>
